<template>
  <transition name="move">
    <div v-show="showFlag" class="food" ref="wrapper">
      <div class="food-content">
        <div class="image-header">
          <img :src="food.image" alt="">
          <div class="back" @click="back">
            <i class="icon-check_circle"></i>
          </div>
        </div>
        <div class="content">
          <h1 class="title">{{food.name}}</h1>
          <div class="detail">
            <span class="sell-count">月售{{food.sellCount}}份</span>
            <span class="rating">好评率{{food.rating}}%</span>
          </div>
          <div class="price">
            <span class="nowPrice">¥{{food.price}}</span>
            <span class="oldPrice" v-show="food.oldPrice">¥{{food.oldPrice}}</span>
          </div>
          <div class="cartcontrol-wrapper" v-show="food.count > 0">
            <cart-control :food="food"></cart-control>
          </div>
          <transition name="fade">
            <div class="buy" v-show="!food.count || food.count ===0" @click.stop.prevent="addFirst()">加入购物车</div>
          </transition>
        </div>
        <split v-show="food.info"></split>
        <div class="info" v-show="food.info">
          <h1 class="title">商品信息</h1>
          <p class="text">{{food.info}}</p>
        </div>
        <split v-show="food.info"></split>
        <div class="rating">
          <h1 class="title">商品评价</h1>
          <rating-select
            @select="selectRating"
            @onlyContent="toggleContent"
            :selectType="selectType"
            :onlyContent="onlyContent"
            :desc="desc"
            :ratings="food.ratings"
          ></rating-select>
          <div class="rating-wrapper">
            <ul v-show="food.ratings && food.ratings.length">
              <li
                v-show="needShow(rating.rateType, rating.text)"
                class="rating-item border-bottom"
                v-for="(rating,index) in food.ratings"
                :key="index"
              >
                <div class="user">
                  <span class="name">{{rating.username}}</span>
                  <img class="avatar" style="width: .24rem;height: .24rem" :src="rating.avatar" alt="">
                </div>
                <div class="time">{{rating.rateTime | formatDate }}</div>
                <p class="text">
                  <span :class="{'icon-thumb_up':rating.rateType === 0, 'icon-thumb_down':rating.rateType === 1}"></span>
                  {{rating.text}}
                </p>
              </li>
            </ul>
            <div class="no-rating" v-show="!food.ratings || !food.ratings.length">暂无任何评价</div>
          </div>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
import Vue from 'vue'
import Bscroll from 'better-scroll'
import cartControl from '../cartcontrol/cartcontrol.vue'
import split from '../split/split.vue'
import ratingSelect from '../ratingselect/ratingselect.vue'
import { formatDate } from '@/common/js/date.js'

const ALL = 2

export default {
  name: 'food',
  props: {
    food: Object
  },
  components: {
    cartControl,
    split,
    ratingSelect
  },
  data () {
    return {
      showFlag: false,
      selectType: ALL,
      onlyContent: false,
      desc: {
        all: '全部',
        positive: '推荐',
        negative: '吐槽'
      }
    }
  },
  methods: {
    show () {
      this.showFlag = true
      this.selectType = ALL
      this.onlyContent = false
      this.$nextTick(() => {
        if (!this.scroll) {
          this.scroll = new Bscroll(this.$refs.wrapper, {
            click: true
          })
        }
        this.scroll && this.scroll.refresh()
      })
    },
    back () {
      this.showFlag = false
    },
    addFirst (food) {
      Vue.set(this.food, 'count', 1)
    },
    selectRating (type) {
      this.selectType = type
      this.$nextTick(() => {
        this.scroll.refresh()
      })
    },
    toggleContent (onlyContent) {
      this.onlyContent = !onlyContent
      this.$nextTick(() => {
        this.scroll.refresh()
      })
    },
    needShow (type, text) {
      if (this.onlyContent && !text) {
        return false
      }
      if (this.selectType === ALL) {
        return true
      } else {
        return type === this.selectType
      }
    }
  },
  filters: {
    formatDate (time) {
      let date = new Date(time)
      return formatDate(date, 'yyyy-MM-dd hh:mm')
    }
  }
}
</script>

<style lang="stylus" scoped>
  .food
    position fixed
    left 0
    top 0
    bottom 1.12rem
    z-index 30
    width 100%
    background #ffffff
    &.move-enter-active, &.move-leave-active
      transform: translate3d(0%, 0, 0)
      transition all .2s linear
    &.move-enter, &.move-leave-to
      transform translate3d(100%,0,0)
    .food-content
      .image-header
        position relative
        width 100%
        height 0
        // 保持了宽和高相等
        padding-bottom 100%
        overflow hidden
        & > img
          position absolute
          left 0
          height 0
          width 100%
          height 100%
        .back
          position absolute
          top .2rem
          left .2rem
          .icon-check_circle
            display block
            padding .2rem
            font-size .8rem
            color #ffffff
      .content
        position relative
        padding .36rem
        .title
          margin-bottom .16rem
          line-height .28rem
          font-size .28rem
          font-weight 700
          color rgb(7,17,27)
        .detail
          margin-bottom .36rem
          line-height .2rem
          height .2rem
          font-size 0
          .sell-count
            font-size .2rem
            color rgb(147,153,159)
            margin-right .24rem
          .rating
            font-size .2rem
            color rgb(147,153,159)
        .price
          font-weight 700
          line-height .48rem
          .nowPrice
            margin-right .16rem
            font-size .28rem
            color rgb(240,20,20)
          .oldPrice
            text-decoration line-through
            font-size .2rem
            color rgb(147,153,159)
        .cartcontrol-wrapper
          position absolute
          right .24rem
          bottom .24rem
        .buy
          position absolute
          right .36rem
          bottom .36rem
          z-index 10
          height .48rem
          line-height .48rem
          padding 0 .24rem
          box-sizing border-box
          border-radius .24rem
          color #ffffff
          background rgb(0,160,220)
          &.fade-enter-active, &.fade-leave-active
            transition opacity .5s
          &.fade-enter, &.fade-leave-to
            opacity 0
      .info
        padding .36rem
        .title
          margin-bottom .12rem
          line-height .28rem
          font-size .28rem
          color rgb(7,17,27)
        .text
          margin 0 .16rem
          line-height .48rem
          font-size .24rem
          font-weight 200
          color rgb(77,85,93)
      .rating
        padding-top .36rem
        .title
          margin-left .36rem
          line-height .28rem
          font-size .28rem
          color rgb(7,17,27)
        .rating-wrapper
          padding 0 .36rem
          .rating-item
            position relative
            padding .32rem 0
            &.border-bottom::before
              border-bottom 1px solid rgba(7,17,27,.2)
            .user
              position absolute
              right -0.12rem
              line-height .24rem
              font-size 0
              .name
                display inline-block
                vertical-align top
                margin-right .12rem
                font-size .2rem
                color rgb(147,153,159)
              .avatar
                border-radius 50%
                width .24rem
                height .24rem
            .time
              margin-bottom .12rem
              line-height .24rem
              font-size .2rem
              color rgb(147,153,159)
            .text
              line-height .32rem
              font-size .24rem
              color rgb(7,17,27)
              .icon-thumb_up
                margin-right .08rem
                line-height .24rem
                font-size .24rem
                color rgb(0,160,220)
              .icon-thumb_down
                margin-right .08rem
                line-height .24rem
                font-size .24rem
                color rgb(147,153,159)
          .no-rating
            padding .32rem 0
            font-size .24rem
            color rgb(147,153,159)
</style>
